package ru.common;

import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamSource;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class StatementEmail {
    private List<String> to = new ArrayList<String>();
    private List<String> cc = new ArrayList<String>();
    private List<String> bcc = new ArrayList<String>();
    private String subject;
    private String body;
    private boolean isHTML = false;
    private List<Attachment> attachments = new ArrayList<Attachment>();

    /**
     * @param to comma-separated list of addresses or null
     * @param cc comma-separated list of addresses or null
     * @param subject email subject
     * @param body text of email 
     */
    public StatementEmail(String to, String cc, String subject, String body) {
        if (to != null) {
            for (String s : to.split(",")) {
                if (!s.matches("\\s*")) {
                    this.to.add(s);
                }
            }
        }
        if (cc != null) {
            for (String s : cc.split(",")) {
                if (!s.matches("\\s*")) {
                    this.cc.add(s);
                }
            }
        }
        this.subject = subject;
        this.body = body;
    }

    public boolean isHTML() {
		return isHTML;
	}

	public void setHTML(boolean isHTML) {
		this.isHTML = isHTML;
	}

	public void addAttachment(String name, InputStreamSource source) {
        attachments.add(new Attachment(name, source));
    }

    public void addAttachment(String name, File source) {
        addAttachment(name, new FileSystemResource(source));
    }

    public void addAttachment(String name, byte[] source) {
        addAttachment(name, new ByteArrayResource(source));
    }

    public void addAttachmentFromClasspath(String name, String source) {
        addAttachment(name, new ClassPathResource(source));
    }

    public void addTo(String to) {
        this.to.add(to);
    }

    public void addCc(String cc) {
        this.cc.add(cc);
    }

    public void addBcc(String bcc) {
        this.bcc.add(bcc);
    }

    public List<String> getTo() {
        return to;
    }

    public List<String> getCc() {
        return cc;
    }

    public List<String> getBcc() {
        return bcc;
    }

    public String getSubject() {
        return subject;
    }

    public String getBody() {
        return body;
    }

    public List<Attachment> getAttachments() {
        return attachments;
    }

    public static class Attachment {
        private String name;
        private InputStreamSource source;

        private Attachment(String name, InputStreamSource source) {
            this.name = name;
            this.source = source;
        }

        public String getName() {
            return name;
        }

        public InputStreamSource getSource() {
            return source;
        }
    }
}
